﻿#region The MIT License (MIT)
// The MIT License (MIT)
// Copyright (c) 2010 - 2013 Sovacode LLC
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace Sovacode.RubiksCube.Common.View
{
    /// <summary>
    /// Class WindowCloseButtonBehavior
    /// </summary>
    public class WindowCloseButtonBehavior : Behavior<Window>
    {
        /// <summary>
        /// The command property
        /// </summary>
        public static readonly DependencyProperty CommandProperty =
          DependencyProperty.Register(
          "Command",
          typeof(ICommand),
          typeof(WindowCloseButtonBehavior));

        /// <summary>
        /// The command parameter property
        /// </summary>
        public static readonly DependencyProperty CommandParameterProperty =
              DependencyProperty.Register(
              "CommandParameter",
              typeof(object),
              typeof(WindowCloseButtonBehavior));

        /// <summary>
        /// The close button property
        /// </summary>
        public static readonly DependencyProperty CloseButtonProperty =
          DependencyProperty.Register(
          "CloseButton",
          typeof(Button),
          typeof(WindowCloseButtonBehavior),
          new FrameworkPropertyMetadata(
                    null,
                    OnButtonChanged));

        /// <summary>
        /// Gets or sets the command.
        /// </summary>
        /// <value>The command.</value>
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        /// <summary>
        /// Gets or sets the command parameter.
        /// </summary>
        /// <value>The command parameter.</value>
        public object CommandParameter
        {
            get { return GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        /// <summary>
        /// Gets or sets the close button.
        /// </summary>
        /// <value>The close button.</value>
        public Button CloseButton
        {
            get { return (Button)GetValue(CloseButtonProperty); }
            set { SetValue(CloseButtonProperty, value); }
        }

        /// <summary>
        /// Called when button changed.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void OnButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var window = ((WindowCloseButtonBehavior)d).AssociatedObject;

            ((Button)e.NewValue).Click += (s, e1) =>
            {
                var command = ((WindowCloseButtonBehavior)d).Command;
                var commandParameter = ((WindowCloseButtonBehavior)d).CommandParameter;
                if (command != null)
                {
                    command.Execute(commandParameter);
                }
                window.Close();
            };
        }
    }
}